package dao;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Morphia;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author AnBoCa
 */
public class DBConnection {
    
    private static DBConnection dbConnection=new DBConnection();
    private Mongo mongo;
    private Morphia morphia;
    private Datastore dataStore;
    private String db;
    private String host;
    private int port;
    
    private DBConnection(){
        Properties properties = new Properties();
        try {
            properties.load(DBConnection.class.getClassLoader().getResourceAsStream("conf/conf.properties"));
        } catch (IOException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.host=properties.getProperty("host");
        this.port=Integer.parseInt(properties.getProperty("port"));
        this.db=properties.getProperty("db");
        try {
            this.mongo=new Mongo(host,port);
            this.morphia=new Morphia();
            this.dataStore=morphia.createDatastore(this.mongo, this.db);
        } catch (UnknownHostException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MongoException ex) {
            Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static DBConnection getInstance(){
        return dbConnection;
    }

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Mongo getMongo() {
        return mongo;
    }

    public void setMongo(Mongo mongo) {
        this.mongo = mongo;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public Datastore getDataStore() {
        return dataStore;
    }

    public void setDataStore(Datastore dataStore) {
        this.dataStore = dataStore;
    }
    
}
